package com.hs.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.order.constant.OrderConstant;
import com.hs.order.pojo.Orders;
import com.hs.order.mapper.OrdersMapper;
import com.hs.order.service.IOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2022-07-20
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Override
    public ResultVo findOrderByConditions(Long orderId, String carLicensePlate, Integer berthId,Integer status) {


        LambdaQueryWrapper<Orders> query = Wrappers.lambdaQuery(Orders.class);
//        判断参数是否为空
        if (orderId != null){
            query.eq(Orders::getId,orderId);
        }
        if (StringUtils.hasText(carLicensePlate)){
            query.eq(Orders::getCarLicensePlate,carLicensePlate);
        }
        if (berthId != null){
            query.eq(Orders::getBerthId,berthId);
        }
        if (status != null){
            query.eq(Orders::getStatus,status);
        }

        List<Orders> orders = this.list(query);

        return new ResultVo(orders);
    }

    @Override
    public ResultVo finishOrder(Long orderId) {
        LambdaUpdateWrapper<Orders> update = Wrappers.lambdaUpdate(Orders.class);
        update.eq(Orders::getId,orderId);
//        修改订单状态为已结束
        update.set(Orders::getStatus, OrderConstant.ORDER_STATUS_FINISH);
        update(update);
        return new ResultVo(ResultCode.SUCCESS);
    }

    @Override
    public ResultVo findUnPayOrders(String tel) {
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        qw.eq("user_tel",tel);
        qw.eq("status",OrderConstant.ORDER_STATUS_UNPAY);
        List<Orders> orders = list(qw);
        return new ResultVo(orders);
    }
}
